package com.example.cursor_orm.dao.impl;

import com.example.cursor_orm.dao.AttributePolicyDAO;
import com.example.cursor_orm.model.AttributePolicy;
import com.example.cursor_orm.util.DatabaseUtil;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class AttributePolicyDAOImpl implements AttributePolicyDAO {
    @Override
    public int insert(AttributePolicy ap) {
        String sql = "INSERT INTO attribute_policy (role_id, permission_id, attribute_key, attribute_value) VALUES (?, ?, ?, ?)";
        return DatabaseUtil.executeUpdate(sql, ap.getRoleId(), ap.getPermissionId(), ap.getAttributeKey(), ap.getAttributeValue());
    }
    @Override
    public int deleteById(Integer id) {
        String sql = "DELETE FROM attribute_policy WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, id);
    }
    @Override
    public int update(AttributePolicy ap) {
        String sql = "UPDATE attribute_policy SET role_id = ?, permission_id = ?, attribute_key = ?, attribute_value = ? WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, ap.getRoleId(), ap.getPermissionId(), ap.getAttributeKey(), ap.getAttributeValue(), ap.getId());
    }
    @Override
    public AttributePolicy findById(Integer id) {
        String sql = "SELECT * FROM attribute_policy WHERE id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, id);
        return mapResultSetToAttributePolicy(rs);
    }
    @Override
    public List<AttributePolicy> findAll() {
        String sql = "SELECT * FROM attribute_policy ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToList(rs);
    }
    @Override
    public List<AttributePolicy> findByCondition(String condition) {
        String sql = "SELECT * FROM attribute_policy WHERE " + condition + " ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToList(rs);
    }
    @Override
    public int count() {
        String sql = "SELECT COUNT(*) FROM attribute_policy";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        try { if (rs != null && rs.next()) return rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); }
        return 0;
    }
    @Override
    public List<AttributePolicy> findByRoleId(Integer roleId) {
        String sql = "SELECT * FROM attribute_policy WHERE role_id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, roleId);
        return mapResultSetToList(rs);
    }
    @Override
    public List<AttributePolicy> findByPermissionId(Integer permissionId) {
        String sql = "SELECT * FROM attribute_policy WHERE permission_id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, permissionId);
        return mapResultSetToList(rs);
    }
    @Override
    public List<AttributePolicy> findByRoleIdAndPermissionId(Integer roleId, Integer permissionId) {
        String sql = "SELECT * FROM attribute_policy WHERE role_id = ? AND permission_id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, roleId, permissionId);
        return mapResultSetToList(rs);
    }
    @Override
    public List<AttributePolicy> findByAttributeKey(String attributeKey) {
        String sql = "SELECT * FROM attribute_policy WHERE attribute_key = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, attributeKey);
        return mapResultSetToList(rs);
    }
    @Override
    public List<AttributePolicy> findByAttributeValue(String attributeValue) {
        String sql = "SELECT * FROM attribute_policy WHERE attribute_value = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, attributeValue);
        return mapResultSetToList(rs);
    }
    @Override
    public List<AttributePolicy> findByAttributeKeyAndValue(String attributeKey, String attributeValue) {
        String sql = "SELECT * FROM attribute_policy WHERE attribute_key = ? AND attribute_value = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, attributeKey, attributeValue);
        return mapResultSetToList(rs);
    }
    @Override
    public int deleteByRoleId(Integer roleId) {
        String sql = "DELETE FROM attribute_policy WHERE role_id = ?";
        return DatabaseUtil.executeUpdate(sql, roleId);
    }
    @Override
    public int deleteByPermissionId(Integer permissionId) {
        String sql = "DELETE FROM attribute_policy WHERE permission_id = ?";
        return DatabaseUtil.executeUpdate(sql, permissionId);
    }
    private AttributePolicy mapResultSetToAttributePolicy(ResultSet rs) {
        try { if (rs != null && rs.next()) {
            AttributePolicy ap = new AttributePolicy();
            ap.setId(rs.getInt("id"));
            ap.setRoleId(rs.getInt("role_id"));
            ap.setPermissionId(rs.getInt("permission_id"));
            ap.setAttributeKey(rs.getString("attribute_key"));
            ap.setAttributeValue(rs.getString("attribute_value"));
            return ap;
        } } catch (SQLException e) { e.printStackTrace(); }
        return null;
    }
    private List<AttributePolicy> mapResultSetToList(ResultSet rs) {
        List<AttributePolicy> list = new ArrayList<>();
        try { if (rs != null) while (rs.next()) {
            AttributePolicy ap = new AttributePolicy();
            ap.setId(rs.getInt("id"));
            ap.setRoleId(rs.getInt("role_id"));
            ap.setPermissionId(rs.getInt("permission_id"));
            ap.setAttributeKey(rs.getString("attribute_key"));
            ap.setAttributeValue(rs.getString("attribute_value"));
            list.add(ap);
        } } catch (SQLException e) { e.printStackTrace(); }
        return list;
    }
} 